﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace BaseSimpleAPI
{
    public class ResponseHeaderHelper
    {
        private ResponseHeaderHelper() { }

        private ThreadLocal<Dictionary<string, string>> _Header = new ThreadLocal<Dictionary<string, string>>();

        /// <summary>
        /// 懒 单例
        /// </summary>
        private static ResponseHeaderHelper _ResponseHeaderHelper = new ResponseHeaderHelper();

        /// <summary>
        /// 获取相关实例
        /// </summary>
        /// <returns></returns>
        public static ResponseHeaderHelper GetInstance()
        {
            return _ResponseHeaderHelper;
        }

        /// <summary>
        /// 获取响应头
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetResponseHeader()
        {
            if (!_Header.IsValueCreated)
                return new Dictionary<string, string>();

            return _Header.Value;
        }

        /// <summary>
        /// 获取响应头
        /// </summary>
        /// <returns></returns>
        public string GetResponseHeader(string key)
        {
            var dic = GetResponseHeader();
            return dic.ContainsKey(key) ? dic[key] : null;
        }

        /// <summary>
        /// 修剪不合适的Key
        /// </summary>
        /// <param name="keys"></param>
        public void TrimResponseHeader(params string[] keys)
        {
            if (!_Header.IsValueCreated)
                return;

            foreach (var item in keys ?? new string[0])
            {
                _Header.Value.Remove(item);
            }
        }

        /// <summary>
        /// 补全响应头（没有就使用推荐值，如果有则不变化）
        /// </summary>
        /// <param name="headers"></param>
        public void CompletionResponseHeader(Dictionary<string, string> headers)
        {
            if (!_Header.IsValueCreated)
            {
                _Header.Value = new Dictionary<string, string>(headers);
                return;
            }


            foreach (var item in headers)
            {
                _Header.Value.TryAdd(item.Key, item.Value);
            }
        }

        /// <summary>
        /// 设置响应头
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void SetResponseHeader(string key, string value)
        {
            if (!_Header.IsValueCreated)
                _Header.Value = new Dictionary<string, string>();

            _Header.Value[key] = value;
        }

        /// <summary>
        /// 设置请求头
        /// </summary>
        /// <param name="values"></param>
        public void SetResponseHeader(params KeyValuePair<string, string>[] values)
        {
            if (!_Header.IsValueCreated)
                _Header.Value = new Dictionary<string, string>();

            foreach (var item in values ?? new KeyValuePair<string, string>[0])
            {
                _Header.Value.TryAdd(item.Key, item.Value);
            }
        }
    }
}
